
*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                           DD ANALYSIS
*====================================================================*


*====================================================================*
* Diarrhea
*====================================================================*

* Merge Datasets First Round
  use "$prodata1\providers_interview_1st", clear

  merge 1:1 prov_id wave using "$prodata1\standardized_patient_diarrhea_b", keepus(case sample2 nhistory dx_type tx_any tx_detail1 tx_type sp_theta_mle_q12_std sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample2
  keep if sample1==1 & sample2==1
  drop sample2
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata1\vignette_diarrhea", keepus(vig_theta_mle_q12_std)
  keep if _merge==3
  drop _merge

  tempfile R1
  save "`R1'"


* Merge Datasets Second Round
  use "$prodata2\providers_interview_2nd", clear
  merge 1:1 prov_id wave using "$prodata2\standardized_patient_diarrhea_b", keepus(case sample2 nhistory dx_type tx_any tx_detail1 tx_type sp_theta_mle_q12_std sp_theta_mle_q12_stdb sp_item? sp_item??)
  keep if _merge==3
  drop _merge

* Checking district in wave 2
  assert cluster == 150 if district==2 & wave==2
  assert cluster == 322 if district==5 & wave==2

  tab sample1 sample2
  keep if sample1==1 & sample2==1
  drop sample2
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata2\vignette_diarrhea", keepus(vig_theta_mle_q12_stdb)
  keep if _merge==3
  drop _merge

  tempfile R2
  save "`R2'"

* Append Rounds
  use "`R1'", clear
  append using "`R2'"
  tab wave

* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Interaction 
  gen treat_post = treat*post

* Percentage of selected items asked
  egen pq_sp_d=rmean(sp_item1-sp_item11)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail1>=3 & tx_detail1<=5 | tx_detail1==7)
  table treat wave, c(mean  tx_harm)

* ORS
  gen     ORS = (tx_detail1<=5)
  lab var ORS "=1 if ORS given"

* Performance Score
  gen     perfscore = sp_theta_mle_q12_std if wave==1
  replace perfscore = sp_theta_mle_q12_stdb if wave==2

* Knowledge Score
/*NOTE: For the second round we need the score without considereing the oversampled WHP*/
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2


* Dependent variable
  local dep = "pq_sp_d perfscore ORS tx_harm"

* Regressors
  local policy "treat post treat_post"
  local reg1 "`policy'"
  local reg2 "`policy' age experience mqualif knowscore"
  local reg3 "`policy' age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Matrix to store results
  mat table3 = J(9,11,.)
  mat colnames table3 = N N_pre mean_pre N_post mean_post beta se ci_l ci_u pvalue adj_pvalue

* Regressions
  local i 1
  foreach var of local dep {
    forvalues r=1/3 {

    * Mean value of dependent variable 
      sum `var' if regsample==1 & post==0
      local npre = r(sum)
      local mean_pre = string(round(r(mean)*100, .1))

      sum `var' if regsample==1 & post==1
      local npost = r(sum)
      local mean_post = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' `reg`r'' if regsample==1, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
        mat table3[`i',1] = e(N)
        mat table3[`i',2] = `npre'
        mat table3[`i',3] = `mean_pre'
        mat table3[`i',4] = `npost'
        mat table3[`i',5] = `mean_post'
        mat table3[`i',6] = _b[treat_post]
        mat table3[`i',7] = _se[treat_post]
        local low_ci  = _b[treat_post] - invttail(e(df_r),0.025)*_se[treat_post]
        local high_ci = _b[treat_post] + invttail(e(df_r),0.025)*_se[treat_post]
        mat table3[`i',8] = `low_ci'
        mat table3[`i',9] = `high_ci'
        local t = _b[treat_post]/_se[treat_post]
        mat table3[`i',10] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_sp_d" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\table3_diarrhea_sp.xls", keep(treat_post) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addnote("District FE") `app'
    }
    local i = `i'+1
  }


* Check: cluster fixed effect instead of treat
  local reg "age experience mqualif knowscore work_hs npatient camps fac_gov niindex"
  mat table3_clusterFE = J(9,11,.)
  mat colnames table3_clusterFE = N N_pre mean_pre N_post mean_post beta se ci_l ci_u pvalue adj_pvalue
  local i 1
  foreach var of local dep {

    * Mean value of dependent variable 
      sum `var' if regsample==1 & post==0
      local npre = r(sum)
      local mean_pre = string(round(r(mean)*100, .1))

      sum `var' if regsample==1 & post==1
      local npost = r(sum)
      local mean_post = string(round(r(mean)*100, .1))

    * Regression
      areg `var' `reg' i.cluster post treat_post if regsample==1, absorb(district) cluster(cluster)

    * Save results
      outreg2 using "$results\table3_diarrhea_sp.xls", keep(treat_post) ctitle("cluster FE",`var') bdec(3) sdec(3) ///
      stats(coef ci_low ci_high pval) noparen nocons append

    * Fill matrix
      mat table3_clusterFE[`i',1] = e(N)
      mat table3_clusterFE[`i',2] = `npre'
      mat table3_clusterFE[`i',3] = `mean_pre'
      mat table3_clusterFE[`i',4] = `npost'
      mat table3_clusterFE[`i',5] = `mean_post'
      mat table3_clusterFE[`i',6] = _b[treat_post]
      mat table3_clusterFE[`i',7] = _se[treat_post]
      local low_ci  = _b[treat_post] - invttail(e(df_r),0.025)*_se[treat_post]
      local high_ci = _b[treat_post] + invttail(e(df_r),0.025)*_se[treat_post]
      mat table3_clusterFE[`i',8] = `low_ci'
      mat table3_clusterFE[`i',9] = `high_ci'
      local t = _b[treat_post]/_se[treat_post]
      mat table3_clusterFE[`i',10] = 2*ttail(e(df_r),abs(`t'))

    local i = `i'+1
  }


* Erase txt files created by outreg
  erase "$results\table3_diarrhea_sp.txt"


*====================================================================*
* Pneumonia
*====================================================================*

* Merge Datasets First Round
  use "$prodata1\providers_interview_1st", clear

  merge 1:1 prov_id wave using "$prodata1\standardized_patient_pneumonia_b", keepus(case sample3 nhistory dx_type tx_any tx_detail3 tx_type sp_theta_mle_q12_std sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample3
  keep if sample1==1 & sample3==1
  drop sample3
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata1\vignette_pneumonia", keepus(vig_theta_mle_q12_std)
  keep if _merge==3
  drop _merge

  tempfile R1
  save "`R1'"


* Merge Datasets Second Round
  use "$prodata2\providers_interview_2nd", clear
  merge 1:1 prov_id wave using "$prodata2\standardized_patient_pneumonia_b", keepus(case sample3 nhistory dx_type tx_any tx_detail3 tx_type sp_theta_mle_q12_std sp_theta_mle_q12_stdb sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample3
  keep if sample1==1 & sample3==1
  drop sample3
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata2\vignette_pneumonia", keepus(vig_theta_mle_q12_stdb)
  keep if _merge==3
  drop _merge

  tempfile R2
  save "`R2'"

* Append Rounds
  use "`R1'", clear
  append using "`R2'"
  tab wave

* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Interaction 
  gen treat_post = treat*post

* Percentage of selected items asked
  egen pq_sp_p=rmean(sp_item1-sp_item9)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail3>=3 & tx_detail3<=5)

* Harmful or non treatment
  gen tx_harm_no=(tx_detail>=3 & tx_detail<=6)

* Antibiotics
  gen antibiotics=(tx_detail<=4)

* Performance Score
  gen     perfscore = sp_theta_mle_q12_std if wave==1
  replace perfscore = sp_theta_mle_q12_stdb if wave==2

* Knowledge Score
/*NOTE: For the second round we need the score without considereing the oversampled WHP*/
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2

* Dependent variable
  local dep = "pq_sp_p perfscore antibiotics tx_harm tx_harm_no"
  local dep = "pq_sp_p perfscore antibiotics tx_harm"

* Regressors
  local policy "treat post treat_post"
  local reg1 "`policy'"
  local reg2 "`policy' age experience mqualif knowscore"
  local reg3 "`policy' age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Regressions
  local i 6
  foreach var of local dep {
    forvalues r=1/3 {

    * Mean value of dependent variable 
      sum `var' if regsample==1 & post==0
      local npre = r(sum)
      local mean_pre = string(round(r(mean)*100, .1))

      sum `var' if regsample==1 & post==1
      local npost = r(sum)
      local mean_post = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' `reg`r'' if regsample==1, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
        mat table3[`i',1] = e(N)
        mat table3[`i',2] = `npre'
        mat table3[`i',3] = `mean_pre'
        mat table3[`i',4] = `npost'
        mat table3[`i',5] = `mean_post'
        mat table3[`i',6] = _b[treat_post]
        mat table3[`i',7] = _se[treat_post]
        local low_ci  = _b[treat_post] - invttail(e(df_r),0.025)*_se[treat_post]
        local high_ci = _b[treat_post] + invttail(e(df_r),0.025)*_se[treat_post]
        mat table3[`i',8] = `low_ci'
        mat table3[`i',9] = `high_ci'
        local t = _b[treat_post]/_se[treat_post]
        mat table3[`i',10] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_sp_p" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\table3_pneumonia_sp.xls", keep(treat_post) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addnote("District FE") `app'
    }
    local i = `i'+1
  }

* Check: cluster fixed effect instead of treat
  local reg "age experience mqualif knowscore work_hs npatient camps fac_gov niindex"
  local i 6
  foreach var of local dep {

    * Mean value of dependent variable 
      sum `var' if regsample==1 & post==0
      local npre = r(sum)
      local mean_pre = string(round(r(mean)*100, .1))

      sum `var' if regsample==1 & post==1
      local npost = r(sum)
      local mean_post = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' `reg' i.cluster post treat_post if regsample==1, absorb(district) cluster(cluster)

    * Save results
      outreg2 using "$results\table3_pneumonia_sp.xls", keep(treat_post) ctitle("cluster FE",`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen append

    * Fill matrix for journal style
      mat table3_clusterFE[`i',1] = e(N)
      mat table3_clusterFE[`i',2] = `npre'
      mat table3_clusterFE[`i',3] = `mean_pre'
      mat table3_clusterFE[`i',4] = `npost'
      mat table3_clusterFE[`i',5] = `mean_post'
      mat table3_clusterFE[`i',6] = _b[treat_post]
      mat table3_clusterFE[`i',7] = _se[treat_post]
      local low_ci  = _b[treat_post] - invttail(e(df_r),0.025)*_se[treat_post]
      local high_ci = _b[treat_post] + invttail(e(df_r),0.025)*_se[treat_post]
      mat table3_clusterFE[`i',8] = `low_ci'
      mat table3_clusterFE[`i',9] = `high_ci'
      local t = _b[treat_post]/_se[treat_post]
      mat table3_clusterFE[`i',10] = 2*ttail(e(df_r),abs(`t'))

    local i = `i'+1
  }


* Erase txt files created by outreg
  erase "$results\table3_pneumonia_sp.txt"

* Export tables
  local matrix "table3 table3_clusterFE"
  foreach mat of local matrix {
  preserve
  drop _all
  svmat2 `mat', names(col)
  foreach var of varlist beta-adj_pvalue {
    replace `var' = round(`var',.001)
  }
  outsheet using "$results\\`mat'.xls", replace
  restore
  }
